package com.mrd.gtimp.business.service;

import com.mrd.base.BaseBiz;
import com.mrd.evaluate.entity.Evaluation;
import com.mrd.exception.QueryException;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimp.business.entity.Declaration;
import com.mrd.gtimp.business.repository.GtimpEvaluationDao;
import com.mrd.spring.repository.BaseRepository;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

/**
 * Created by lenovo on 2016/9/5.
 */
@Service
public class GtimpEvaluationBiz extends BaseBiz<Evaluation> {

    @Resource
    private GtimpEvaluationDao gtimpEvaluationDao;

    @Resource
    private DeclarationBiz declarationBiz;

    private Map<String, Evaluation> findEvaluationMapWithDeclarationIds(List<String> declarationIds) {
        List<Evaluation> evaluations = gtimpEvaluationDao.findByRelationIdIn(declarationIds);
        Map<String, Evaluation> evaluationMap = new HashMap<>();
        for (Evaluation evaluation : evaluations) {
            evaluationMap.put(evaluation.getRelationId(), evaluation);
        }
        return evaluationMap;
    }

    public Page<Declaration> findDeclarationList(Jqgrid jqgrid) {
        Page<Declaration> declarationPage = null;
        try {
            List<Evaluation> evaluationList = super.findAllEntity();
            List<String> relationIds = evaluationList.stream().map(Evaluation::getRelationId).collect(Collectors.toList());
            Map<String, Evaluation> evaluationMap = findEvaluationMapWithDeclarationIds(relationIds);
            String temp = "";
            for (String relationId : relationIds) {
                temp += relationId + ",";
            }
            if (StringUtils.isNotEmpty(temp)) {
                temp = temp.substring(0, temp.length() - 1);
                jqgrid.addRule(new SearchRule("id", SearchRuleOption.in, Arrays.asList(temp.split(","))));
            }
            jqgrid.addOrder(Sort.Direction.DESC, "ctime");
            declarationPage = declarationBiz.findByJqgrid(jqgrid);
            List<Declaration> declarationList = declarationPage.getContent();
            declarationList.stream().forEach(declaration -> declaration.setEvaluation(evaluationMap.get(declaration.getId())));
            declarationPage = new PageImpl<>(declarationList, jqgrid.getPage(), declarationPage.getTotalElements());
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return declarationPage;
    }

    @Resource
    // 该处指定注入BaseRepository的dao对象
    @Qualifier("gtimpEvaluationDao")
    @Override
    public void setBaseRepository(BaseRepository<Evaluation, String> baseRepository) {
        super.baseRepository = baseRepository;
    }
}
